package org.yejiali.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yejiali.common.constants.SystemConstants;
import org.yejiali.common.response.ResponseResult;
import org.yejiali.domain.entity.Article;
import org.yejiali.domain.entity.Category;
import org.yejiali.domain.vo.ArticleDetailVo;
import org.yejiali.domain.vo.ArticleListVo;
import org.yejiali.domain.vo.HotArticleVo;
import org.yejiali.domain.vo.PageVo;
import org.yejiali.mapper.ArticleMapper;
import org.yejiali.service.ArticleService;
import org.yejiali.service.CategoryService;
import org.yejiali.util.RedisCache;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 文章表(Article)表服务实现类
 *
 * @author yejiali
 * @since 2025-02-14 16:30:36
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisCache redisCache;
    @Override
    public ResponseResult hotArticleList() {
        //查询热门文章 封装成ResponeResult返回
        LambdaQueryWrapper<Article> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //必须是正式文章
        lambdaQueryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        //浏览量排序
        lambdaQueryWrapper.orderByDesc(Article::getViewCount);
        //最多十条
        Page<Article> page=new Page<>(1,10);
        page(page,lambdaQueryWrapper);
        List<Article> articles = page.getRecords();
        //返回数据
        //bean拷贝
        List<HotArticleVo> articleVos = BeanUtil.copyToList(articles, HotArticleVo.class);
        return ResponseResult.okResult(articleVos);
    }

    @Override
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {
        //查询条件
        LambdaQueryWrapper<Article> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //如果有id,查询时要和传入相同(这里的查询条件都是并列，若Id没有,则不对id进行查询)
        lambdaQueryWrapper.eq(Objects.nonNull(categoryId)&&categoryId>0,Article::getCategoryId,categoryId);
        //文章状态为已发布
        lambdaQueryWrapper.eq(Article::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL);
        //置顶文章降序排序
        lambdaQueryWrapper.orderByDesc(Article::getIsTop);
        //进行分页(这里还应该检查参数是否为空)
        Page<Article> page=new Page<>(pageNum,pageSize);
        page(page,lambdaQueryWrapper);
        //封装查询结果
        //将categoryName查询出来
        List<Article> articles = page.getRecords();
        /*for (Article article:articles) {
            Category category = categoryService.getById(article.getCategoryId());
            article.setCategoryName(category.getName());
        }*/
        //获取分类id,查询分类名称
        articles=articles.stream()
                .map(article -> {
                    Category category = categoryService.getById(article.getCategoryId());
                    article.setCategoryName(category.getName());
                    return article; })
                .collect(Collectors.toList());

        List<ArticleListVo> articleListVos = BeanUtil.copyToList(articles, ArticleListVo.class);
        PageVo pageVo = new PageVo(articleListVos, page.getTotal());
        //查询分类名
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult getArticleDetail(Long id) {
        //根据id查询文章
        Article article = getById(id);
        //从redis中获取文章当前浏览量
        Integer viewCount = redisCache.getCacheObject(id.toString());
        article.setViewCount(viewCount.longValue());
        //转换成VO
        ArticleDetailVo articleDetailVo = BeanUtil.copyProperties(article, ArticleDetailVo.class);
        //根据分类id查询分类名
        Category category = categoryService.getById(articleDetailVo.getCategoryId());
        if(category!=null){
            articleDetailVo.setCategoryName(category.getName());
        }
        //封装响应返回
        return ResponseResult.okResult(articleDetailVo);
    }

    /**
     * 更新浏览量时更新相应的数据库
     * @param id
     * @return
     */
    @Override
    public ResponseResult updateViewCount(Long id) {
        redisCache.incrementHashMapValue("article:viewCountMap",id.toString(),1L);
        return ResponseResult.okResult();
    }
}

